package com.jhclothing.web.controller.product;

import com.jhclothing.common.annotation.Log;
import com.jhclothing.common.core.controller.BaseController;
import com.jhclothing.common.core.domain.AjaxResult;
import com.jhclothing.common.core.page.TableDataInfo;
import com.jhclothing.common.enums.BusinessType;
import com.jhclothing.common.enums.ClothingSex;
import com.jhclothing.common.utils.StringUtils;
import com.jhclothing.common.utils.poi.ExcelUtil;
import com.jhclothing.product.domain.ClothingType;
import com.jhclothing.product.domain.Fabric;
import com.jhclothing.product.service.IClothingTypeService;
import com.jhclothing.product.service.IFabricService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 服饰品类Controller
 *
 * @author zyp
 * @date 2024-04-21
 */
@RestController
@RequestMapping("/product/clothingType")
public class ClothingTypeController extends BaseController {
    @Autowired
    private IClothingTypeService clothingTypeService;

    @Autowired
    private IFabricService fabricService;

    /**
     * 查询服饰品类列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ClothingType clothingType) {
        startPage();
        List<ClothingType> list = clothingTypeService.selectClothingTypeList(clothingType);
        setFabricManufactures(list);
        return getDataTable(list);
    }

    /**
     * 导出服饰品类列表
     */
    @PreAuthorize("@ss.hasPermi('product:clothingType:export')")
    @Log(title = "服饰品类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClothingType clothingType) {
        List<ClothingType> list = clothingTypeService.selectClothingTypeList(clothingType);
        setFabricManufactures(list);
        ExcelUtil<ClothingType> util = new ExcelUtil<ClothingType>(ClothingType.class);
        util.exportExcel(response, list, "服饰品类数据");
    }

    /**
     * 获取服饰品类详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:clothingType:query')")
    @GetMapping(value = {"/", "/{clothingTypeId}"})
    public AjaxResult getInfo(@PathVariable(value = "clothingTypeId", required = false) Long clothingTypeId) {
        AjaxResult ajax = AjaxResult.success();
        List<Fabric> fabrics = fabricService.selectFabricAll();
        ajax.put("fabrics", fabrics);
        if (StringUtils.isNotNull(clothingTypeId)) {
            ClothingType clothingType = clothingTypeService.selectClothingTypeByClothingTypeId(clothingTypeId);
            ajax.put(AjaxResult.DATA_TAG, clothingType);
            ajax.put("fabricIds", clothingType.getFabrics().stream().map(Fabric::getFabricId).collect(Collectors.toList()));
        }
        return ajax;
    }

    /**
     * 新增服饰品类
     */
    @PreAuthorize("@ss.hasPermi('product:clothingType:add')")
    @Log(title = "服饰品类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClothingType clothingType) {
        if (!clothingTypeService.checkClothingTypeCodeAndSexUnique(clothingType)) {
            String clothingTypeCode = clothingType.getClothingTypeCode();
            String clothingSexName = ClothingSex.getClothingSexNameByCode(clothingType.getClothingSex());
            return error(String.format("新增品类编码为%s, 服饰类别为%s的服饰品类失败, 当前品类编码和服饰类别的服饰品类已存在",
                    clothingTypeCode, clothingSexName));
        }
        clothingType.setCreateBy(getUsername());
        return toAjax(clothingTypeService.insertClothingType(clothingType));
    }

    /**
     * 修改服饰品类
     */
    @PreAuthorize("@ss.hasPermi('product:clothingType:edit')")
    @Log(title = "服饰品类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClothingType clothingType) {
        if (!clothingTypeService.checkClothingTypeCodeAndSexUnique(clothingType)) {
            String clothingTypeCode = clothingType.getClothingTypeCode();
            String clothingSexName = ClothingSex.getClothingSexNameByCode(clothingType.getClothingSex());
            return error(String.format("修改品类编码为%s, 服饰类别为%s的服饰品类失败, 当前品类编码和服饰类别的服饰品类已存在",
                    clothingTypeCode, clothingSexName));
        }
        clothingType.setUpdateBy(getUsername());
        return toAjax(clothingTypeService.updateClothingType(clothingType));
    }

    /**
     * 删除服饰品类
     */
    @PreAuthorize("@ss.hasPermi('product:clothingType:remove')")
    @Log(title = "服饰品类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{clothingTypeIds}")
    public AjaxResult remove(@PathVariable Long[] clothingTypeIds) {
        return toAjax(clothingTypeService.deleteClothingTypeByClothingTypeIds(clothingTypeIds));
    }

    /**
     * 设置服饰品类对应的布料厂商信息
     *
     * @param clothingTypeList 服饰品类列表
     */
    private void setFabricManufactures(List<ClothingType> clothingTypeList) {
        clothingTypeList.forEach(clothingType -> {
            Set<String> fabricManufacturer = clothingType.getFabrics().stream()
                    .map(Fabric::getFabricManufacturer)
                    .collect(Collectors.toSet());
            String fabricManufacturers = String.join(", ", fabricManufacturer);
            clothingType.setFabricManufacturers(fabricManufacturers);
        });
    }
}
